﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.IO.Compression;
using System.Windows.Forms;
using System.Threading;


namespace InternetCheck
{
    public class InternetCheck
    {
        // uri
        private Uri uri;

        // 状态值
        public enum ResultType : int
        {
            OK = 1,
            NotURL = 0,
            NotFound = 404,
            NotSite = -1,
            NotWeb = -2,
            Forbidden = -3
        }

        public InternetCheck()
        {
        }

        public ResultType CheckWeibo(string url)
        {
            ResultType result = ResultType.NotFound;

            if (string.IsNullOrWhiteSpace(url) || (
                !url.StartsWith(@"https://", StringComparison.OrdinalIgnoreCase) &&
                !url.StartsWith(@"http://", StringComparison.OrdinalIgnoreCase)))
                return ResultType.NotURL;

            uri = new Uri(url);
            if(uri == null)
                return ResultType.NotURL;

            try
            {
                if (uri.AbsoluteUri.StartsWith(@"https://weibo.com", StringComparison.OrdinalIgnoreCase))
                {
                    //HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
                    //myHttpWebRequest.Credentials = CredentialCache.DefaultCredentials;
                    //HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
                    //if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
                    //{
                    //    result = ResultType.OK;
                    //}
                    //else
                    //    result = ResultType.NotWeb;
                    //myHttpWebResponse.Close();
                    result = ResultType.OK;
                }
                else
                    result = ResultType.NotSite;
                return result;
            }
            catch (WebException ee)
            {
                Debug.WriteLine("WebException Raised. The following error occured : " + ee.Status);
                return result;
            }
            catch (Exception ee)
            {
                Debug.WriteLine("The following Exception was raised : " + ee.Message);
                return result;
            }
        }

        public int CheckWeibo(string[] urls = null)
        {
            if (urls == null)
                return 0;

            int i = 0;

            foreach (string url in urls)
            {
                if (CheckWeibo(url) == ResultType.OK)
                    i++;
            }
            return i;
        }
    }
}
